﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHTU;
using NOMFinalSettlement.Classes;
using NOMFinalSettlement.AppForm;
using System.Data.SqlClient;

namespace NOMFinalSettlement
{
    public partial class MainApp : Office2007RibbonForm
    {
        #region "Khai báo dùng chung"
        // Define MainApp
        public static MainApp Self;

        public NHTULicense lic = null;

        // Define app
        public AppProcess appProcess = new AppProcess();

        //Hỏi trước khi thoát chương trình
        public bool askExit = true;

        public SqlConnectionStringBuilder connInfo;

        public FinalSettlementModel.User DBUSER = new FinalSettlementModel.User();

        public Loading loading;

        public fLoading loadingForm = new fLoading();

        public WorkerThread bwWorker = null;

        #endregion

        #region "Xử lý Form Main"

        public MainApp()
        {
            // Apply Main App
            Self = this;

            InitializeComponent();

            // Make sure client has permissions 
            appProcess.clientPermission();
        }

        private void MainApp_Load(object sender, EventArgs e)
        {
            bwWorker = new WorkerThread(System.Windows.Threading.Dispatcher.CurrentDispatcher);//ref WindowsBase
            bwWorker.WorkerReportsProgress = true;
            bwWorker.DoWork += MyDoWork;
            bwWorker.ProgressChanged += MyProgressChanged;
            bwWorker.RunWorkerCompleted += RunWorkerCompleted;


            Properties.Settings.Default.Reload();
            FinalSettlementModel.Properties.Settings.Default.Reload();

            __setPointGroupInfo();

            appProcess.setIsLogin(false);

            appProcess.setStartUp(Properties.Settings.Default.AppStartUp);

            appStyle.ManagerStyle = Properties.Settings.Default.AppSkin;

            connInfo = new SqlConnectionStringBuilder();
            connInfo = SQLServer.ConnectionStringDeCrypt(FinalSettlementModel.Properties.Settings.Default.ConnectionString);

            lic = new NHTULicense();
            lic.Register(45, true);

            appProcess.callFormDialog("AppForm.fLogin");
                   
        }

        private void MainApp_Resize(object sender, EventArgs e)
        {
            __setPointGroupInfo();
        }
        #endregion

        #region "Xử lý thoát phần mềm"

        public void restartApp()
        {
            Self.Close();
            Application.Restart();
            Application.ExitThread();
        }

        private void appMenuExit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
                Application.ExitThread();
                Application.Exit();
            }
            catch { }
        }

        private void MainApp_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                Application.ExitThread();
                Application.Exit();
            }
            catch { }
        }

        private void MainApp_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.askExit == false) return;
            if (MsgBoxResult.Yes != MsgBox.Show(
            "Bạn thật sự muốn thoát khỏi chương trình?",
            "Thoát chương trình",
             MsgBoxButton.YesNo,
             MsgBoxIcon.Question))
            {
                e.Cancel = true;
            }            
        }

        #endregion

        #region "Menu click thay đổi giao diện"
        private void Office2007Blue_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Office2007Blue);
        }

        private void Office2007Silver_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Office2007Silver);
        }

        private void Office2007Black_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Office2007Black);
        }

        private void Office2007VistaGlass_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Office2007VistaGlass);
        }

        private void Office2010Blue_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Office2010Blue);
        }

        private void Office2010Silver_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Office2010Silver);
        }

        private void Office2010Black_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Office2010Black);
        }

        private void Windows7Blue_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Windows7Blue);
        }

        private void VisualStudio2010Blue_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.VisualStudio2010Blue);
        }

        private void Metro_Click(object sender, EventArgs e)
        {
            appProcess.setStyle(eStyle.Metro);
        }
        #endregion

        #region "Help action"
        private void btnAboutUs_Click(object sender, EventArgs e)
        {
                
            appProcess.callFormDialog("AppForm.fAbout");
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            appProcess.callFormInsideTab("AppForm.fHelp");
        }
        #endregion

        #region "Sự kiện gọi form"

        private void appMenuLogout_Click(object sender, EventArgs e)
        {
            appProcess.setIsLogin(false);
            appProcess.removeAllTabs(tabControl);
            appProcess.callFormDialog("AppForm.fLogin");
        }

        private void appMenuLogin_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.fLogin");
        }

        private void appSetting_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.App.fSetting");
        }
        
        private void btnUserManager_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.App.fUser");
            }
        }

        private void appMenuChangePassword_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.App.fChangePassword");
        }

        private void btnAcceptInvestment_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.Program.fAcceptInvestment");
        }

        private void btnInvestors_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.Program.fInvestors");
        }

        private void btnChooseConstruction_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.Program.fChooseConstruction");
        }

        private void btnExamineUser_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.Program.fExamineUser");
        }

        private void btnCapitalInvestment_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.Program.fCapitalInvestment");
        }

        private void btnSignFinally_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fSignFinally");
            }
        }

        private void btnAddNewProfile_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fAddNewProfile");
            }
        }

        private void btnApplyProfile_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fApplyProfile");
            }
        }

        private void btnApplySign_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fApplySign");
            }
        }

        private void btnGroupProjects_Click(object sender, EventArgs e)
        {
            appProcess.callFormDialog("AppForm.Program.fGroupProjects");
        }

        private void btnReportExport_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fReportExport");
            }
        }

        private void btnReportAll_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fReportAll");
            }
        }
        private void btnReportNewProfile_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fReportNewProfile");
            }
        }

        private void btnReportNotFinish_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fReportNotFinish");
            }
        }

        private void btnReportNotFinishDetails_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fReportNotFinishDetails");
            }
        }

        private void btnReportFinishDetails_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fReportFinishDetails");
            }
        }

        private void appMenuNew_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fAddNewProfile");
            }
        }

        private void appMenuOpen_Click(object sender, EventArgs e)
        {
            if (bwWorker.IsBusy != true)
            {
                bwWorker.RunWorkerAsync("AppForm.Program.fReportExport");
            }
        }

        private void btnDataBackup_Click(object sender, EventArgs e)
        {
            int tabCount = tabControl.Tabs.Count;
            if (tabCount > 0)
            {
                if (MsgBoxResult.Yes != MsgBox.Show(
                    "Thao tác này yêu cầu đóng các màn hình làm việc hiện có.\nBạn có muốn tiếp tục không?",
                    "Cảnh báo.",
                     MsgBoxButton.YesNo,
                     MsgBoxIcon.Question))
                {
                    return;
                }
                appProcess.removeAllTabs(tabControl);
            }
            appProcess.callFormDialog("AppForm.App.fBackup");
        }

        private void btnDataRestore_Click(object sender, EventArgs e)
        {
            int tabCount = tabControl.Tabs.Count;
            if (tabCount > 0)
            {
                if (MsgBoxResult.Yes != MsgBox.Show(
                    "Thao tác này yêu cầu đóng các màn hình làm việc hiện có.\nBạn có muốn tiếp tục không?",
                    "Cảnh báo.",
                     MsgBoxButton.YesNo,
                     MsgBoxIcon.Question))
                {
                    return;
                }
                appProcess.removeAllTabs(tabControl);
            }
            appProcess.callFormDialog("AppForm.App.fRestore");
        }

        #endregion
        
        #region "Xử lý Work Background"
        public void MyDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            string task = e.Argument.ToString();
            appProcess.callFormInsideTab(task);
        }

        public void MyProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            
        }

        public void RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {

        }

        #endregion

        #region "Các hàm dùng chung"
        void __setPointGroupInfo()
        {
            //Load groupLogin center TabPanel form
            int groupWidth = this.groupInfoCompany.Width;
            int groupHeight = this.groupInfoCompany.Height;
            int x = (this.Width - groupWidth) / 2;
            int y = (this.Height - groupHeight) / 2;
            if (this.Height < 600)
            {
                y = y + (y / 3);
            }

            this.groupInfoCompany.Location = new Point(x, y);
        }
        #endregion

        #region "Xử lý TabControl"

        private void menuCloseAllTab_Click(object sender, EventArgs e)
        {
            appProcess.removeAllTabs(tabControl);
        }

        private void tabControl_TabRemoved(object sender, EventArgs e)
        {
            appProcess.setTabCloseItem(tabControl);
        }

        private void tabControl_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ctxTabControl.Show(tabControl, e.Location);
            }
        }

        private void tabControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ctxTabControl.Show(tabControl, e.Location);
            }
        }

        private void tabControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ctxTabControl.Show(tabControl, e.Location);
            }
        }
        #endregion

    }
}
